package arm_search_3.data;

import java.util.ArrayList;
import java.util.Arrays;

/**
 *
 * @author
 */
public class QueryModel {

    public void setTerms(ArrayList<String> terms) {
        this.terms = terms;
    }

    public ArrayList<String> getTerms() {
        return terms;
    }

    public void setField(String field) {
        this.field = field;
    }

    public void setIsPhrase(boolean isPhrase) {
        this.isPhrase = isPhrase;
    }

    public String getField() {
        return field;
    }

    public boolean isIsPhrase() {
        return isPhrase;
    }
    private String field;
    private boolean isPhrase;
    private ArrayList<String> terms;

    @Override
    public String toString() {
        return "[" + field + ", " + isPhrase + "]" + terms;
    }

    public QueryModel(String queryString, String field) {
        this.field = field;
        this.isPhrase = isPhrase(queryString);
        terms = new ArrayList<>();
        
        if (isPhrase) {
            queryString = queryString.replaceAll("\"", "");
        } else {
            queryString = queryString.replaceAll("\'", "");
        }
        String splitted[] = queryString.split(" ");
        terms.addAll(Arrays.asList(splitted));
    }

    public static ArrayList<QueryModel> splitString(String fullQuery) {
        ArrayList<QueryModel> result = new ArrayList<>();
        fullQuery = normalizeString(fullQuery);
        int iter;
        StringBuilder builder;
        for (iter = 0; iter < fullQuery.length(); iter++) {
            try {
                String tempQuery = "";
                String tempField = "all";
                if (fullQuery.charAt(iter) == '\'') {
                    builder = new StringBuilder("\'");
                    iter++;
                    while ((iter < fullQuery.length()) && (fullQuery.charAt(iter) != '\'')) {
                        builder.append(fullQuery.charAt(iter));
                        iter++;
                    }
                    builder.append("\'");
                    tempQuery = builder.toString();
                    builder = new StringBuilder();
                    iter++;
                    if (fullQuery.charAt(iter) == ':') {
                        iter++;
                        while ((iter < fullQuery.length()) && (fullQuery.charAt(iter) != ' ')) {
                            builder.append(fullQuery.charAt(iter));
                            iter++;
                        }
                        tempField = builder.toString();
                    }
                } else if (fullQuery.charAt(iter) == '\"') {
                    builder = new StringBuilder("\"");
                    iter++;
                    while ((iter < fullQuery.length()) && (fullQuery.charAt(iter) != '\"')) {
                        builder.append(fullQuery.charAt(iter));
                        iter++;
                    }
                    builder.append("\"");
                    tempQuery = builder.toString();
                    builder = new StringBuilder();
                    iter++;
                    if (fullQuery.charAt(iter) == ':') {
                        iter++;
                        while ((iter < fullQuery.length()) && (fullQuery.charAt(iter) != ' ')) {
                            builder.append(fullQuery.charAt(iter));
                            iter++;
                        }
                        tempField = builder.toString();
                    }
                } else {
                    builder = new StringBuilder();
                    while ((iter < fullQuery.length()) && (fullQuery.charAt(iter) != ' ')) {
                        builder.append(fullQuery.charAt(iter));
                        iter++;
                    }
                    String temp = builder.toString();
                    String tempArr[] = temp.split(":");
                    if (tempArr.length == 2) {
                        tempField = tempArr[1];
                    }
                    tempQuery = tempArr[0];
                }
                result.add(new QueryModel(tempQuery, tempField));
            } catch (Exception ex) {
            }
        }
        return result;
    }

    public static boolean isPhrase(String queryString) {
        return ((queryString.charAt(0) == '"') && (queryString.charAt(queryString.length() - 1) == '"'));
    }
    

    /**
     * menormalkan string dengan menghapus tanda baca
     *
     * @param string string input
     * @return string yang telah dihapus dari tanda baca
     */
    private static String normalizeString(String string) {
        int i;
        StringBuilder builder = new StringBuilder();
        char[] charArr = string.toLowerCase().toCharArray();
        for (i = 0; (i < charArr.length); i++) {
            if (charArr[i] == '-' || charArr[i] == '=' || charArr[i] == '`' || charArr[i] == '\'' || charArr[i] == '$' || charArr[i] == '=' || charArr[i] == '#') {
            } else {
                switch (charArr[i]) {
                    case '<': {
                        i++;
                        if (i == charArr.length) {
                        } else {
                            while ((i < charArr.length) && (charArr[i] != '>')) {
                                i++;
                            }
                        }
                        break;
                    }
                    case '\\': {
                        builder.append(' ');
                        break;
                    }
                    case '/': {
                        builder.append(' ');
                        break;
                    }
                    case '_': {
                        builder.append(' ');
                        break;
                    }
                    case '+': {
                        builder.append(' ');
                        break;
                    }
                    case '*': {
                        builder.append(' ');
                        break;
                    }
                    case '.': {
                        builder.append(' ');
                        break;
                    }
                    case '@': {
                        builder.append(' ');
                        break;
                    }
                    case '!': {
                        builder.append(' ');
                        break;
                    }
                    case '?': {
                        builder.append(' ');
                        break;
                    }
                    case ',': {
                        builder.append(' ');
                        break;
                    }
                    case ';': {
                        builder.append(' ');
                        break;
                    }
                    case '(': {
                        builder.append(' ');
                        break;
                    }
                    case ')': {
                        builder.append(' ');
                        break;
                    }
                    case '{': {
                        builder.append(' ');
                        break;
                    }
                    case '}': {
                        builder.append(' ');
                        break;
                    }
                    case '[': {
                        builder.append(' ');
                        break;
                    }
                    case ']': {
                        builder.append(' ');
                        break;
                    }
                    case '|': {
                        builder.append(' ');
                        break;
                    }
                    default: {
                        builder.append(charArr[i]);
                    }
                }
            }
        }
        return builder.toString();
    }
}
